package ru.pb.game.data.xml.parsers;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import ru.pb.game.data.xml.holders.ChannelHolder;
import ru.pb.game.objects.model.CHANNEL;
import ru.pb.game.objects.model.enums.CHANNEL_TYPE;
import ru.pb.global.utils.ParserUtils;
import ru.pb.global.xml.parsers.AbstractFileParser;

public class ChannelParser extends AbstractFileParser<ChannelHolder>
{
	private static ChannelParser _instance;

	public static ChannelParser getInstance()
	{
		if(_instance == null)
			_instance = new ChannelParser();
		return _instance;
	}

	private ChannelParser()
	{
		super("./data/templates/channelTemplate.xml", ChannelHolder.getInstance());
	}

	@Override
	protected void readData(Node start0, String fileName)
	{
		if("list".equalsIgnoreCase(start0.getNodeName()))
		{
			for(Node n = start0.getFirstChild(); n != null; n = n.getNextSibling())
			{
				int id;
				CHANNEL_TYPE type;
				String text = "";

				if(n.getNodeName().equalsIgnoreCase("channel"))
				{
					NamedNodeMap attr = n.getAttributes();
					id = ParserUtils.intValue(attr.getNamedItem("id"));
					type = ParserUtils.enumValue(attr.getNamedItem("type"), CHANNEL_TYPE.class, CHANNEL_TYPE.BEGIN1);

					for(Node s = n.getFirstChild(); s != null; s = s.getNextSibling())
					{
						if("message".equalsIgnoreCase(s.getNodeName()))
						{
							NamedNodeMap attr2 = s.getAttributes();
							text = ParserUtils.stringValue(attr2.getNamedItem("text").getNodeValue(), "");
						}
					}
					getHolder().addChannel(new CHANNEL(id, type, text));
				}
			}
		}
	}
}
